﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *  
 **/

using System;
using System.ComponentModel.Composition;
using com.microsoft.tfs.core;
using com.microsoft.tfs.core.clients.framework.location;
using com.microsoft.tfs.core.clients.versioncontrol.soapextensions;
using com.microsoft.tfs.core.httpclient;
using com.microsoft.tfs.core.util;
using java.net;
using Peter.Common.Code;
using Peter.Common.Interfaces;
using Peter.Common.Utilities;
using URI = com.microsoft.tfs.core.httpclient.URI;

namespace Peter.Tfs
{
   /// <summary>
   /// Source control for TFS.
   /// </summary>
   public class TfsSourceControl : ISourceControl
   {
      private IPeter m_Peter;
      private readonly CommandPrompt m_BgCmd;

      /// <summary>
      /// Initializes a new source control
      /// </summary>
      public TfsSourceControl ()
      {
         this.m_BgCmd = new CommandPrompt ();
         this.m_BgCmd.OutputRecieved += this.OnBgCmdOutput;
         this.m_BgCmd.Initialize ();

         TFSTeamProjectCollection tpc = null;
         Credentials credentials;

         var userName = "jpmon1_cp";
         // In case no username is provided and the current platform supports
         // default credentials, use default credentials
         if ((userName == null || userName.Length == 0) && CredentialsUtils.supportsDefaultCredentials ())
         {
            credentials = new DefaultNTCredentials ();
         }
         else
         {
            credentials = new UsernamePasswordCredentials (userName, "uh197697");
         }
         /*URI httpProxyURI = null;

         if (HTTP_PROXY_URL != null && HTTP_PROXY_URL.length () > 0)
         {
            try
            {
               httpProxyURI = new URI (HTTP_PROXY_URL);
            }
            catch (URISyntaxException e)
            {
               // Do Nothing
            }
         }*/

         //ConsoleSamplesConnectionAdvisor connectionAdvisor = new ConsoleSamplesConnectionAdvisor (httpProxyURI);

         //tpc = new TFSTeamProjectCollection (URIUtils.newURI (COLLECTION_URL), credentials, connectionAdvisor);
         tpc = new TFSTeamProjectCollection (URIUtils.newURI ("https://tfs.codeplex.com/tfs/TFS03"), credentials);

         Workspace workspace = tpc.getVersionControlClient ().tryGetWorkspace (@"D:\Work\Code\Peter\dev\src\Peter\");
         //return tpc;
      }

      /// <summary>
      /// Gets the instance of peter.
      /// </summary>
      [Import (typeof (IPeter))]
      public IPeter Peter
      {
         get { return this.m_Peter; }
         set
         {
            this.m_Peter = value;
            this.m_Peter.ShuttingDown += this.OnPeterShutdown;
         }
      }

      /// <summary>
      /// Occurs when we get output from the tf exe.
      /// </summary>
      /// <param name="message"></param>
      /// <param name="iserror"></param>
      private void OnBgCmdOutput (string message, bool iserror)
      {
         
      }

      /// <summary>
      /// Called when Peter is shutting down.
      /// </summary>
      /// <param name="sender">Peter object.</param>
      /// <param name="e">EventArgs</param>
      private void OnPeterShutdown (object sender, EventArgs e)
      {
         this.m_BgCmd.Close ();
      }
   }
}
